<?php
class User_AuthController extends Promotor_Controller_Action {

	public $skin = array(
		// specjalizuje template dla akcji
		'login' => array(
			'layout' => 'login'
		)
	);

	/**
	 * @return void
	 */
	public function indexAction() {
		$this->_forward('login');
	}

	/**
	 * @return void
	 */
	public function loginAction() {
		$rq = $this->getRequest();
		$form = new User_Form_Login();

		if (!$rq->isPost()) {
			$this->view->form = $form;
			return;
		}

		if (!$form->isValid($rq->getPost())) {
			$this->view->form = $form;
			return;
		}
		
		$username = $form->getSubForm('auth')->getValue('username');
		$password = $form->getSubForm('auth')->getValue('password');

		$model = new User_Model_Auth();
		$model->login($username, $password);

		$flashMessenger = $this->_helper->getHelper('flashMessenger');
//		$flashMessenger->addMessage(User_Model_Auth::saltPassword($username, $password));

		$status = $model->getStatus();
		if ($status == User_Model_Auth::SUCCESS) {
			$flashMessenger->addMessage($status);
			$url = $this->_helper->url->url(array());
			$this->_helper->redirector->goToUrlAndExit($url);
		} else {
			$messages = $model->getMessages();
			foreach ($messages as $message) {
				$flashMessenger->addMessage($message);
			}
			$this->view->form = $form;
		}
	}
	
	/**
	 * @return void
	 */
	public function logoutAction() {
		Zend_Auth::getInstance()->clearIdentity();
		$this->_redirect('/');
	}
}